/**
 * 定义 用户管理 相关的接口
 */
import request from '@/utils/request'
import type {
  AssignRoleData,
  RoleListResponseData,
  UserInfo,
  UserListResponseData,
} from '@/api/acl/user/type'

/**
 * 接口地址定义
 */
enum API {
  // 获取全部管理用户列表的接口地址
  GET_USER_LIST_URL = '/admin/acl/user/',
  // 添加用户
  ADD_USER_URL = '/admin/acl/user/save',
  // 修改用户
  UPDATE_USER_URL = '/admin/acl/user/update',
  // 获取全部角色与当前用户所属角色的接口地址
  ALL_ROLE_URL = '/admin/acl/user/toAssign/',
  // 给已有的用户分配角色
  ASSIGN_ROLE_URL = '/admin/acl/user/doAssignRole',
  // 删除用户
  DELETE_USER_URL = '/admin/acl/user/remove/',
  // 批量删除用户
  BATCH_DELETE_USERS_URL = '/admin/acl/user/batchRemove',
}

/**
 * 获取全部管理用户列表的接口
 */
export const reqGetUserList = (
  page: number | string,
  limit: number | string,
  username: string,
) => {
  return request.get<any, UserListResponseData>(
    API.GET_USER_LIST_URL + `${page}/${limit}/?username=${username}`,
  )
}

/**
 * 添加或修改用户
 */
export const reqAddOrUpdateUser = (data: UserInfo) => {
  if (data.id) {
    return request.put<any, any>(API.UPDATE_USER_URL, data)
  } else {
    return request.post<any, any>(API.ADD_USER_URL, data)
  }
}

/**
 * 获取全部角色与当前用户所属角色的接口
 */
export const reqAllRole = (userId: number | string) => {
  return request.get<any, RoleListResponseData>(API.ALL_ROLE_URL + userId)
}

/**
 * 给已有的用户分配角色
 */
export const reqAssignRole = (data: AssignRoleData) => {
  return request.post<any, any>(API.ASSIGN_ROLE_URL, data)
}

/**
 * 删除用户
 */
export const reqDeleteUser = (userId: number | string) => {
  return request.delete<any, any>(API.DELETE_USER_URL + userId)
}

/**
 * 批量删除用户
 */
export const reqBatchDeleteUsers = (idList: number[]) => {
  return request.delete<any, any>(API.BATCH_DELETE_USERS_URL, { data: idList })
}
